HTMLify
app.js
Views: 15 | Author: huxn-webdev
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | const url = "https://api.thecatapi.com/v1/images/search"; const section = document.querySelector(".container"); const button = document.querySelector(".btn"); button.addEventListener("click", getRandomCats); randomCatPhoto = (json) => { let photo = json[0].url; section.classList.add("cats"); let image = document.createElement("img"); image.src = photo; image.classList.add("random_cats"); image.alt = photo; section.appendChild(image); }; async function getRandomCats() { section.innerHTML = ""; try { const response = await fetch(url); const json = await response.json(); console.log("JSON:", json); return randomCatPhoto(json); } catch (e) { console.log("This is an error"); console.log(e); } } |